#include <REGX52.H>
#include <INTRINS.H>

//引脚定义 SPI通信的作用
sbit XPY2046_DIN=P3^4;
sbit XPY2046_CS=P3^5;
sbit XPY2046_DCLK=P3^6;
sbit XPY2046_DOUT=P3^7;

/**
  * @brief  ZPT2046读取AD值
  * @param  Command 命令字，范围：头文件内定义的宏，结尾的数字表示转换的位数
  * @retval AD转换后的数字量，范围：8位为0~255，12位为0~4095
  */
unsigned int XPT2046_ReadAD(unsigned int Command)
 {
	 unsigned char i =0;
	 unsigned int Data = 0x0000;
	 //写一个字节的Command
	 XPY2046_DCLK = 0;
	 XPY2046_CS = 0;
	for(i=0;i<8;i++)
	{
		
		XPY2046_DIN = Command&(0x80>>i);
		XPY2046_DCLK = 1;
		XPY2046_DCLK = 0;
	}
	
	//读一个字节的Data
	for(i=0;i<16;i++)
	{
		XPY2046_DCLK = 1;
		XPY2046_DCLK = 0;
		if(XPY2046_DOUT)
		{
			Data = Data|(0x8000>>i);//16位
		}
	}
	XPY2046_CS=1;
	if(Command&0x08)
	{
		return Data>>8;
	}
	else
	{
		return Data>>4;
	}
	
 }